var mongoose = require('mongoose');
var qiniu = require('node-qiniu');
var db = require('../config/db');
var config = require('../config/config');
var fs = require("fs");
var async = require('async');
var path = require('path');


// mongodb
//'mongodb://username:password@host:port/database?options...'
var connection = mongoose.connect(db.mongodb.db, function (err) {
    // 链接错误
    if (err) {
        console.error('connect to %s error: ', db.mongodb.db, err.message);
        process.exit(1);
    }else{
        console.log('已连接数据库');
    }
});


var autoIncrement = require('mongoose-auto-increment');   //自增ID 模块
autoIncrement.initialize(connection);

module.exports.autoIncrement = autoIncrement;

require('./admin_action');
require('./admin_group');
require('./admin_user');
require('./ads');
require('./ads_category');
require('./article');
require('./article_category');
require('./shop_goods');
require('./shop_goods_category');
require('./shop_goods_shop');
require('./shop_seller');
require('./shop_goods_spec');
require('./shop_goods_spec_item');
require('./shop_goods_status');
require('./shop_orders');
require('./vip_orders');
require('./sys_config');
require('./store_pay');
require('./store_seller');
require('./user');
require('./user_address');
require('./user_level');
require('./user_union');
require('./logistics');
require('./account_details');
require('./wechat_menu');
require('./qrcodes');
require('./store');
require('./key_reply');
require('./reply_content');
require('./wepay_logs');
require('./wechat_media');
require('./ttt');
require('./zhaopin');
require('./baoming');
require('./dxaname');
require('./coupon');


exports.AdminAction = mongoose.model('admin_action');
exports.AdminGroup = mongoose.model('admin_group');
exports.AdminUser = mongoose.model('admin_user');
exports.Ads = mongoose.model('ads');
exports.AdsCategory = mongoose.model('ads_category');
exports.Article = mongoose.model('article');
exports.ArticleCategory = mongoose.model('article_category');
exports.ShopGoods = mongoose.model('shop_goods');
exports.ShopGoodsCategory = mongoose.model('shop_goods_category');
exports.ShopGoodsShop = mongoose.model('shop_goods_shop');
exports.ShopSeller = mongoose.model('shop_seller');
exports.ShopGoodsSpec = mongoose.model('shop_goods_spec');
exports.ShopGoodsSpecItem = mongoose.model('shop_goods_spec_item');
exports.ShopGoodsStatus = mongoose.model('shop_goods_status');
exports.ShopOrders = mongoose.model('shop_orders');
exports.VipOrders = mongoose.model('vip_orders');
exports.SysConfig = mongoose.model('sys_config');
exports.StorePay = mongoose.model('store_pay');
exports.StoreSeller = mongoose.model('store_seller');
exports.User = mongoose.model('user');
exports.Store = mongoose.model('store');
exports.UserAddress = mongoose.model('user_address');
exports.UserLevel = mongoose.model('user_level');
exports.UserUnion = mongoose.model('user_union');
exports.Logistics = mongoose.model('logistics');
exports.AccountDetails = mongoose.model('account_details');
exports.WechatMenu = mongoose.model('wechat_menu');
exports.Qrcodes = mongoose.model('qrcodes');
exports.KeyReply = mongoose.model('key_reply');
exports.ReplyContent = mongoose.model('reply_content');
exports.WepayLogs = mongoose.model('wepay_logs');
exports.WechatMedia = mongoose.model('wechat_media');
exports.TTT = mongoose.model('ttt');
exports.Zhaopin = mongoose.model('zhaopin');
exports.Baoming = mongoose.model('baoming');
exports.Dxaname = mongoose.model('dxaname');
exports.Coupon = mongoose.model('coupon');


//七牛上传处理
qiniu.config({
    access_key : config.qiniu.ACCESS_KEY,
    secret_key : config.qiniu.SECRET_KEY
});
var imagesBucket = qiniu.bucket(config.qiniu.bucket);
exports.uploadQiniu = function (ImgFile,cb) {
    console.log('qiuniu',ImgFile);
    imagesBucket.putFile(ImgFile, __dirname + '/../public/'+ImgFile, function(err, reply) {
        console.log(reply);
        if (err) {
            cb('/'+ImgFile);
        }else{
            fs.unlink( __dirname + '/../public/'+ImgFile);
            cb('http://'+config.hostName+'/'+ImgFile);
        }
    });
};
